package com.example.demo.controller;

import com.example.demo.pojo.User;
import com.example.demo.service.UserService;
import com.example.demo.utils.Md5Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: 关注微信公众号《web项目开发》
 */

@Api(description = "用户接口")
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @RequestMapping("test")
    @CrossOrigin
    public Map<String, Object> test() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("msg", "成功啦");
        return map;
    }

    @ApiOperation(value = "用户注册", notes = "用户注册")
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @CrossOrigin
    public Map<String, Object> register(@RequestParam String userName,
                                        @RequestParam String passwd) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(passwd)) {
            map.put("msg", "用户名和密码不能为空");
            return map;
        }
        // 验证用户名是否已经注册
        User exsitUser = userService.selectByName(userName);
        if (exsitUser != null) {
            map.put("msg", "该用户名已存在!");
            return map;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(new Date());
        User user = new User();
        user.setUserName(userName);
        user.setRegistTime(format);
        user.setStatus("1");
        user.setPasswd(Md5Util.MD5(passwd) + userName);
        user.setPower("no");
        int count = userService.insert(user);
        System.out.println(count);
        if (count != 1) {
            map.put("msg", "注册失败");
            return map;
        }
        map.put("msg", "注册成功");
        return map;
    }

    @ApiOperation(value = "用户登录", notes = "用户登录")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @CrossOrigin
    public Map<String, Object> login(@RequestParam String userName,
                                     @RequestParam String passwd,
                                     HttpSession session,
                                     HttpServletResponse response) {
        System.out.println("用户名和密码"+userName+passwd);
        Map<String, Object> map = new HashMap<String, Object>();
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        //封装用户的登录数据
        UsernamePasswordToken token = new UsernamePasswordToken(userName,Md5Util.MD5(passwd) + userName);

        try{
            subject.login(token);//执行登录方法,如果没有异常就说明OK
        }catch (UnknownAccountException e){//用户名不存在
            map.put("msg","用户名错误");
            return map;
        }catch (IncorrectCredentialsException e){//密码不存在
            map.put("msg","密码错误");
            return map;
        }
        User User = userService.selectByName(userName);
        session.setAttribute("loginUser", User );
        map.put("msg", "登录成功");
        map.put("user", User);
        return map;
//        Map<String, Object> map = new HashMap<String, Object>();
//        User exsitUser = userService.selectByName(userName);
//        if (exsitUser == null) {
//            map.put("msg", "该用户未注册");
//            return map;
//        }
//        if (!exsitUser.getPasswd().equals(Md5Util.MD5(passwd) + userName)) {
//            map.put("msg", "密码错误,请重新输入");
//            return map;
//        }
//        System.out.println(exsitUser.getPower());
//        if(exsitUser.getPower().equals("no")) {
//            map.put("msg", "无登录权限");
//            return map;
//        }
//        session.setAttribute("loginUser", exsitUser);
//        map.put("msg", "登录成功");
//        return map;
    }

    @RequestMapping("/user")
    public User user(){
        return new User();
    }

    @RequestMapping("/toLogin")
    public Map<String, Object> toLogin(){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("msg", "无登录权限");
        return map;
    }

    @RequestMapping("/noPower")
    public Map<String, Object> noPower(){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("msg", "无访问权限");
        return map;
    }

}
